using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Evaluation3.utils;
using Evaluation3.data;
using Evaluation3.classes;

public partial class uicontrol_THIO_DRAW : System.Web.UI.UserControl
{
    private double t = 0;
    private double h = 0;
    private double i = 0;
    private double o = 0;
    private double total = 0;
    public static double MAX_THIO = 1.0;
    public static double MAX_TOTAL_THIO = 1;
    public static int MAX_HIGHT = 200;
    private string description = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        
        companyClass objCompany = (companyClass)Session[keyUtils.SES_COMPANY];
        if (objCompany != null) cb_company.SelectedValue = objCompany.Id;
        productData.SelectParameters.UpdateValues(this.Context, cb_company);
        //DrawingSample(); 
    }

    public void DrawingSample(string ti, string hi, string ii, string oi) {
        T = double.Parse(ti.Replace(",","."));
        if (T > 1) T = T / 10;
        setT_Value(ti);
        H = double.Parse(hi.Replace(",", ".")) ;
        if (H > 1) H = H / 10;
        setH_Value(hi);
        I = double.Parse(ii.Replace(",", ".")) ;
        if (I > 1) I = I / 10;
        setI_Value(ii);
        O = double.Parse(oi.Replace(",", "."));
        if (O > 1) O = O / 10;
        setO_Value(oi);
        TOTAL = T + H + I + O;
        setTotal_Value(Convert.ToString(TOTAL / MAX_TOTAL_THIO).Replace(",","."));
    }
    protected string DESCIPTION
    {
        set { description = value; }
        get { return description; }
    }
    protected string getDescription()
    {
        return DESCIPTION;
    }
    protected int getMAX_HIGHT()
    {
        return MAX_HIGHT;
    }
    protected double T
    {
        set { t = value; }
        get { return t; }
    }

    protected string getTperMax()
    {
        return (MAX_HIGHT / 2 * T).ToString() + "%";
    }
    protected double H
    {
        set { h = value; }
        get { return h; }
    }
    protected string getHperMax()
    {
        return (MAX_HIGHT / 2 * H).ToString() + "%";
    }
    protected double I
    {
        set { i = value; }
        get { return i; }
    }
    protected string getIperMax()
    {
        return (MAX_HIGHT / 2 * I).ToString() + "%";
    }
    protected double O
    {
        set { o = value; }
        get { return o; }
    }
    protected string getOperMax()
    {
        return (MAX_HIGHT / 2 * O).ToString() + "%";
    }
    protected double TOTAL
    {
        set { total = value; }
        get { return total; }
    }
    protected string getTotal_Value()
    {
        return txtTotal.Text;
    }
    protected string setTotal_Value(String vtt)
    {
        return txtTotal.Text = vtt;
    }

    protected string getT_Value()
    {
        return txtT.Text;
    }
    protected string getTotalPerMax()
    {
        if (TOTAL == 4) return "100%";
        return Convert.ToString(TOTAL / 4) + "%";
    }
    protected string setT_Value(String vt)
    {
        return txtT.Text = vt;
    }
    protected string getH_Value()
    {
        return txtH.Text;
    }
    protected string setH_Value(String vh)
    {
        return txtH.Text = vh;
    }
    protected string getI_Value()
    {
        return txtI.Text;
    }
    protected string setI_Value(String vi)
    {
        return txtI.Text = vi;
    }
    protected string getO_Value()
    {
        return txtO.Text;
    }
    protected string setO_Value(String vo)
    {
        return txtO.Text = vo;
    }



    public void loadData(String companyId, String productId) {
        Double BetaT = 0;
        Double BetaH = 0;
        Double BetaI = 0;
        Double BetaO = 0;
        Double Tmark = 0;
        Double Hmark = 0;
        Double Imark = 0;
        Double Omark = 0;

        Double TongDoanhThuQ = 0;
        Double LamDa = 0;
        Double Tau = 0;

        Double TBT = 1;
        Double HBH = 1;
        Double IBI = 1;
        Double OBO = 1;
        Double TCN = 1;
    try
    {
        TongDoanhThuQ = Convert.ToDouble(dataUtils.EXECUTESCALAR("select Q from TieuChuanKyThuat where MaSanPham='" + productId + "'"));
        BetaT = Convert.ToDouble(dataUtils.EXECUTESCALAR("select BetaT from TieuChuanKyThuat where MaSanPham='" + productId + "'"));
        BetaH = Convert.ToDouble(dataUtils.EXECUTESCALAR("select BetaH from TieuChuanKyThuat where MaSanPham='" + productId + "'"));
        BetaI = Convert.ToDouble(dataUtils.EXECUTESCALAR("select BetaI from TieuChuanKyThuat where MaSanPham='" + productId + "'"));
        BetaO = Convert.ToDouble(dataUtils.EXECUTESCALAR("select BetaO from TieuChuanKyThuat where MaSanPham='" + productId + "'"));
        LamDa = Convert.ToDouble(dataUtils.EXECUTESCALAR("Select avg(Lamda) from NhanToMoiTruong where MaSanPham= " + productId + "'"));
        Tmark = Convert.ToDouble(dataUtils.EXECUTESCALAR("TinhTForDoanhNghiep " + companyId + "," + productId + ""));
        Hmark = Convert.ToDouble(dataUtils.EXECUTESCALAR("TinhHForDoanhNghiep " + companyId + "," + productId + ""));
        Imark = Convert.ToDouble(dataUtils.EXECUTESCALAR("TinhIForDoanhNghiep " + companyId + "," + productId + ""));
        Omark = Convert.ToDouble(dataUtils.EXECUTESCALAR("TinhOForDoanhNghiep " + companyId + "," + productId + ""));

    }
    catch (Exception e) { 
    
    }   
        TBT = Math.Pow(Tmark, BetaT);        
        HBH = Math.Pow(Hmark, BetaH);
        IBI=Math.Pow(Imark,BetaI);
        OBO=Math.Pow(Omark,BetaO);
        Tau=TBT*HBH*IBI*OBO;
        TCN=  LamDa *  Tau *TongDoanhThuQ;

        DrawingSample(TBT.ToString(), HBH.ToString(), IBI.ToString(), OBO.ToString());
        
        
    }

    protected void cb_product_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadData(cb_company.SelectedValue, cb_product.SelectedValue);
    }
    protected void cb_company_SelectedIndexChanged(object sender, EventArgs e)
    {
        productData.SelectParameters.UpdateValues(this.Context, cb_company);
    }
    protected void btnClose_Click(object sender, EventArgs e)
    {
        Response.Redirect("default.aspx");
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        DrawingSample(getT_Value(),getH_Value(),getI_Value(),getO_Value());        
         
    }
}
